#include "Animation.hpp"
#include "TextureManager.hpp"
#include "../Game/Game.hpp"


Animation::Animation(std::string spritesheetpath, unsigned int frames_count, float delay,  bool looping) : m_framescount(frames_count), m_delay(delay), m_loop(looping)
{
	m_spritesheet.setTexture(*(TextureManager::Instance()->Get(spritesheetpath)));
	m_currentframe = 0;
	m_playing = false;
}

Animation::~Animation()
{
}

void Animation::update(){
	if(m_playing){
		if(m_delay > 0){
			m_currentframe = (unsigned int)(m_timer.getElapsedTime().asMilliseconds()/m_delay);
			if(m_currentframe > m_framescount && !m_loop){
				this->stop();
			}else{
				m_currentframe = m_currentframe % m_framescount;
			}
		}
	}
	sf::Vector2u textureSize = m_spritesheet.getTexture()->getSize();
	int frame_w = textureSize.x / m_framescount;
	m_spritesheet.setTextureRect(sf::IntRect(frame_w * m_currentframe, 0, frame_w, textureSize.y));
}

void Animation::draw(){
	update();
	Game::Instance()->Window().draw(m_spritesheet);
}